package com.blacktry.service.impl;

import com.blacktry.entity.model.AnswerNumValue;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.Admin;
import com.blacktry.service.AutoInfoService;
import com.blacktry.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class AutoInfoServiceImpl implements AutoInfoService {
    @Autowired
    private AdminService adminService;

    @Override
    public String autoClass(String username) {
        Admin admin = adminService.selectByUsername(username);
        if (null==admin){
            return "数据库暂无该成员详细信息--"+username;
        }
        return admin.getAffiliation();
    }

    @Override
    public String autoName(String username) {
       Admin admin = adminService.selectByUsername(username);
        if (null==admin){
            return "数据库暂无该成员详细信息--"+username;
        }
        return admin.getName();
    }

    @Override
    public String autoUsername(String data) {
        StringBuffer result = new StringBuffer();
        for (int i = data.length() - 12; i < data.length(); i++) {
            char cScan = data.charAt(i);
            if(cScan == 'l'){
                result.append(1);
            }else if(cScan == 'o' || cScan == 'O'){
                result.append(0);
            }else{
                result.append(cScan);
            }
        }
        System.out.println("\n\n自动转换的学号为：" + result.toString() + "\n\n");
        return result.toString();
    }

    @Override
    public String autoMultiQ(String multiQ) {
        multiQ = multiQ.trim();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < multiQ.length(); i++) {
            char cScan = multiQ.charAt(i);
            if(cScan == 'o' || cScan == 'O'){
                result.append('D');
            }else{
                result.append(cScan);
            }
        }
        System.out.println("\n\n自动转换的选择题为：" + result.toString() + "\n\n");
        return result.toString();
    }

    @Override
    public String autoJudgeQ(String judgeQ) {
        judgeQ = judgeQ.trim();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < judgeQ.length(); i++) {
            char cScan = judgeQ.charAt(i);
            if(cScan == '丁'){
                result.append('T');
            }else{
                result.append(cScan);
            }
        }
        System.out.println("\n\n自动转换的判断题为：" + result.toString() + "\n\n");
        return result.toString();
    }

    @Override
    public String autoObjScore(String scanAnswer, String answer, AnswerNumValue answerNumValue) {
        System.out.println("识别："+scanAnswer);
        System.out.println("答案："+answer);
        int objScore = 0;
        for (int i = 0; i < answer.length(); i++) {
            char cAnswer = answer.charAt(i);
            char cScan = scanAnswer.charAt(i);
            // 选择题部分
            if (cAnswer <= 'D' && cAnswer >= 'A') {
                if (cAnswer == cScan) {
                    objScore+=answerNumValue.getXuanzeNum(); // todo 设置选择题分数
                }
            }else{
                if (cAnswer == cScan) {
                    objScore+=answerNumValue.getPanduanNum(); // todo 设置判断题分数
                }
            }
        }
        return String.valueOf(objScore);
    }

    @Override
    public String autoRightData(List<String> data,String answer) {
        int length = answer.length();
        StringBuffer scanData = new StringBuffer();
        StringBuffer rightData = new StringBuffer();
        for (String datum : data) {
            scanData.append(datum);
        }
        for (int i = 0; i < length; i++) {
            if(isMQ(answer.charAt(i))){
                if(isMQ(scanData.charAt(i))){
                    rightData.append(scanData.charAt(i));
                }
            }else if(isJQ(answer.charAt(i))){
                if(isJQ(scanData.charAt(i))){
                    rightData.append(scanData.charAt(i));
                }
            }
        }
        return rightData.toString();
    }


    // 算法改进2.0版本
    public List<String> autoData2RightData(List<String> data, String answer){
        char cTemp; // 定义一个中转字符
        int currentLength = 0; //当前字符串剩余长度
        int queueCount = -1; //第几个串 由于进循环就++ 所以最开始是-1
        String dataQueue = new String();
        int i = 0;
        StringBuffer rightData = new StringBuffer();

        //学号判断
        int ansSQ = 12;   //12位学号
        while (ansSQ != 0) {
            if (currentLength == 0 || i == dataQueue.trim().length()) {
                queueCount++;
                i = 0;
                dataQueue = data.get(queueCount).trim();
                currentLength = data.get(queueCount).trim().length();
            }
            if (dataQueue.charAt(i) != ' ') {
                cTemp = dataQueue.charAt(i);
                cTemp = autoBlurUsernameChar(cTemp);
                if (isDig(cTemp)) {
                    rightData.append(cTemp);
                    ansSQ--;
                } else if (isMQ(cTemp)) {    //读到了选择的字符 但学号没完全被填充 退出直接进选择题循环
                    System.out.println("注意！学号不完整!");  //学号不完整 后续模糊
                    break;
                }
            }
            currentLength--;
            i++;
            if (ansSQ == 0) break; //直接跳出防止选择读完剩余字符串中有判断中的字符
        }

        rightData.append(" ");

        int length = answer.length();
        int ansMQ = 0, ansJQ = 0;
        for (int j = 0; j < length; j++) {
            if (isMQ(answer.charAt(j))) {
                ansMQ++;
            } else if (isJQ(answer.charAt(j))) {
                ansJQ++;
            }
        }
        while (ansMQ != 0) {
            if (currentLength == 0 || i == dataQueue.trim().length()) {
                queueCount++;
                i = 0;
                dataQueue = data.get(queueCount).trim();
                currentLength = data.get(queueCount).trim().length();
            }
            if (dataQueue.charAt(i) != ' ') {
                cTemp = dataQueue.charAt(i);
                cTemp = autoBlurMQChar(cTemp);
                if (isMQ(cTemp)) {
                    rightData.append(cTemp);
                    ansMQ--;
                } else if (isJQ(cTemp)) {  //读到了判断的字符 但选择题答案没完全被填充 退出直接进判断题循环
                    System.out.println("注意！选择答案不完整"); //提示
                    break;
                }
            }
            currentLength--;
            i++;
            if (ansMQ == 0) break; //直接跳出防止选择读完剩余字符串中有判断中的字符
        }

        rightData.append(" ");

        while (ansJQ != 0) {
            if (currentLength == 0 || i == dataQueue.trim().length()) {
                queueCount++;
                if (queueCount >= data.size()) {    //所有读完  判断还没完(如果存在 主观题有TF 的情况会误读)
                    System.out.println("注意！判断答案不完整");  //提示
                    break;
                }
                i = 0;
                dataQueue = data.get(queueCount).trim();
                currentLength = data.get(queueCount).trim().length();
            }
            if (dataQueue.charAt(i) != ' ') {
                cTemp = dataQueue.charAt(i);
                cTemp = autoBlurJQChar(cTemp);
                if (isJQ(cTemp)) {
                    rightData.append(cTemp);
                    ansJQ--;
                }
            }
            currentLength--;
            i++;
            if (ansJQ == 0) break;
        }
        return Arrays.asList(rightData.toString().split(" "));
    }

    // 选择题
    public boolean isMQ(char c){
        return c<='D'&&c>='A';
    }

    // 判断题
    public boolean isJQ(char c){
        return c=='T'||c=='F';
    }

    // 数字
    public  boolean isDig(char c) {
        return c <= '9' && c >= '0';
    }

    // 模糊处理选择题字符
    public char autoBlurMQChar(char c){
        if(c == 'o' || c == 'O'){
            return 'D';
        }
        return c;
    }
    // 模糊处理判断题字符
    public char autoBlurJQChar(char c){
        if(c == '丁'){
            return 'T';
        }
        return c;
    }
    // 模糊处理学号字符
    public char autoBlurUsernameChar(char c){
        if(c == 'l'){
            return '1';
        }else if(c == 'o' || c == 'O'){
            return '0';
        }
        return c;
    }

}
